package com.yuma.tingshu.search.api;

import com.yuma.tingshu.common.result.Result;
import com.yuma.tingshu.query.search.AlbumIndexQuery;
import com.yuma.tingshu.search.service.SearchService;
import com.yuma.tingshu.vo.search.AlbumInfoIndexVo;
import com.yuma.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    /**
     * 首页显示
     * 查询一级分类下置顶三级分类的热门频道数据
     * <p>
     * 根据指定的①多个三级分类 ID，查询②每个分类下③热度最高的前 6 条专辑信息
     *
     * @param category1Id
     * @return
     */
    @GetMapping("channel/{category1Id}")
    public Result<List<Map<String, Object>>> channel(@PathVariable Long category1Id) throws IOException {
        List<Map<String, Object>> maps = this.searchService.channel(category1Id);
        return Result.ok(maps);
    }

    /**
     * 分页查询专辑
     * <p>
     * 在分类路径为 2→104→1019 的专辑中,
     * 筛选出属性 1 的值为 2,
     * 且标题 / 简介 / 播音员名称包含"音乐"的内容,
     * 按热度从高到低取前 10 条,
     * 同时高亮显示标题中的"音乐"关键词,
     * 并隐藏不必要的字段
     *
     * @param query
     */
    @PostMapping
    public Result<AlbumSearchResponseVo> search(@RequestBody AlbumIndexQuery query) {
        AlbumSearchResponseVo responseVo = this.searchService.search(query);
        return Result.ok(responseVo);
    }

    /**
     * 根据搜索关键字产生相关提示词
     * <p>
     * 分别针对中文前缀、拼音前缀和拼音首字母缩写前缀进行联想推荐
     *
     * @param keyword
     * @return
     */
    @GetMapping("completeSuggest/{keyword}")
    public Result<List<String>> completeSuggest(@PathVariable String keyword) {
        List<String> suggesters = this.searchService.completeSuggest(keyword);
        return Result.ok(suggesters);
    }

    /**
     * 根据一级分类标题和排行榜类型获取排行榜列表
     *
     * @param category1Id
     * @param statType
     * @return
     */
    @GetMapping("findRankingList/{category1Id}/{statType}")
    public Result<List<AlbumInfoIndexVo>> findRankingList(@PathVariable Long category1Id, @PathVariable String statType) {
        List<AlbumInfoIndexVo> albumInfoIndexVos = this.searchService.findRankingList(category1Id, statType);
        return Result.ok(albumInfoIndexVos);
    }

    /**
     * 排行榜同步到ES
     *
     * @return
     */
    @PostMapping("syncDataToEs")
    public Result syncDataToEs() {
        this.searchService.syncDataToEs();
        return Result.ok();
    }
}

